﻿using HslCommunication;
using NetCore8Demo.Models.PLC;
using NetCore8Demo.Tool;
using NetCore8Demo.ViewModels.PLC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCore8Demo.BusinessData
{

    /// <summary>
    /// 常用的封装方法
    /// </summary>
    public class CommonTool
    {
        /// <summary>
        /// 无论是什么设备、什么参数配置，都可以加载
        /// </summary>
        public void SafeLoad()
        {
            StaticTool.LoadAllJsonParam();
            StaticTool.InitSerialPortToolList();
            StaticTool.InitPlcToolList();
        }


        /// <summary>
        /// 发送串口数据的方法
        /// </summary>
        /// <param name="serialPortName"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public OperateResult<string> SerialPortSendData(SerialPortName serialPortName,string data="")
        {
            OperateResult<string> operateResult = new OperateResult<string>();
            if((int)serialPortName > StaticTool.SerialPortToolList.Count-1)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "串口信息越界，请查看编辑界面";
                return operateResult;
            }
            if ("".Equals(data))
            {
                operateResult = StaticTool.SerialPortToolList[(int)serialPortName].SendDefaultData();
            }
            else 
            {
                operateResult = StaticTool.SerialPortToolList[(int)serialPortName].SendData(data);
            }
            return operateResult;
        }



        #region PLC相关操作

        PlcReadAndWriteDemo _plc;


        public void InitPlc()
        {

        }

       



        #endregion





    }
}
